/*
		A Local system display module for the desktop2 system, displaying uptime and cpu
		
		Author: 	Matthew Allpress
		Created:	12/07/06
		Edited:		24/09/06

*/
var SystemInfoBackend = "http://localhost:5050/local/op=get&sysinfo";
var sys_mod_name = "System Stats";

var toAdd = "";
toAdd+= "<div style=\"position: absolute; width: 200px; height: 90px; z-index: 1; right: 50px; top: 800px\" id=\"sysInfo\">\n";
toAdd+= "<div style=\"position: absolute; width: 200px; height: 20px; z-index: 1; left: 25px; top: 5px; background-color:#00FFFF \" id=\"memBackGround\">\n";
toAdd+= "<\/div>\n";
toAdd+= "<div style=\"position: absolute; width: 200px; height: 20px; z-index: 3; left: 25px; top: 5px;\" id=\"memBack\">\n";
toAdd+= "<p align=\"center\"><b><font face=\"Courier\">Ram 0 \/ 0 MB<\/font><\/b>\n";
toAdd+= "<\/div>\n";
toAdd+= "<div style=\"position: absolute; width: 200px; height: 20px; z-index: 2; left: 25px; top: 5px;\" id=\"memBar\">\n";
toAdd+= "<\/div>\n";
toAdd+= "<div style=\"position: absolute; width: 200px; height: 20px; z-index: 2; left: 25px; top: 35px; background-color:#00FFFF\" id=\"upTime\">\n";
toAdd+= "<\/div>\n";
toAdd+= "<\/div>\n";

document.body.innerHTML+=toAdd;
toAdd = null;

var temp_element = document.getElementById("sysInfo");

temp_element.style.top = module_config[sys_mod_name]['top'];
if(module_config[sys_mod_name]['left'] != null)
{
	temp_element.style.left = module_config[sys_mod_name]['left'];
}
else
{
	temp_element.style.right = module_config[sys_mod_name]['right'];
}

document.getElementById("memBackGround").style.backgroundColor = module_config[sys_mod_name]['bgc'];
document.getElementById("upTime").style.backgroundColor = module_config[sys_mod_name]['bgc'];
document.getElementById("memBar").style.backgroundColor = module_config[sys_mod_name]['fcolour'];

temp_element = null;

makeNewRequest("GET", SystemInfoBackend, SystemInfoChange);
window.setInterval("makeNewRequest(\"GET\", SystemInfoBackend, SystemInfoChange);", 10000);

function SystemInfoChange() 
{
 // If data received correctly
 if (XMLRequestObject.readyState == 4) 
 {
  //alert(RSSRequestObject.responseText);
  // if data is valid
  
  if (XMLRequestObject.responseText.indexOf('invalid') == -1) 
  { 	
		   // Parsing RSS
		var node = XMLRequestObject.responseXML.documentElement; 
	   // Get Channel information
		var pram_total = node.getElementsByTagName('pram_total').item(0).firstChild.data;
		var pram_avail = node.getElementsByTagName('pram_avail').item(0).firstChild.data;
		var up_time = node.getElementsByTagName('up_time').item(0).firstChild.data;
	
		pram_total = Math.round((pram_total / 1048576) + 0.5);
		pram_avail = Math.round((pram_avail / 1048576) + 0.5);
	   
		document.getElementById("memBack").innerHTML = "<p align=\"center\"><b><font face=\"Courier\">RAM " + pram_avail + " / " + pram_total + " MB<\/font><\/b>\n";
		document.getElementById("memBar").style.width = Math.round((pram_avail / pram_total) * 200);
		document.getElementById("upTime").innerHTML = "<p align=\"center\"><b><font face=\"Courier\">Up Time: " + calc_uptime(up_time) + "<\/font><\/b>\n";
		
		node = null;
		pram_total = null;
		pram_avil = null;
  }
  else 
  {
   // Tell the reader that there was error requesting data
   alert("ERROR (System Module): There was an error requesting the data");
  }
 }
}

function calc_uptime(time)
{
	var days = 0;
	var hours = 0;
	var minutes = 0;
	time = Math.floor(time / 1000);
	days = Math.floor(time / 86400);
	time = Math.floor(time % 86400);
	hours = Math.floor(time / 3600);
	time = Math.floor(time % 3600);
	minutes = Math.floor(time / 60);
	toReturn = days + "D " + hours + "H " + minutes + "M";
	time = null;
	days = null;
	hours = null;
	minutes = null;
	return(toReturn);
}